class Solution2 {
    public String multiply(String num1, String num2) {
        if(num1.charAt(0) == '0' || num2.charAt(0) == '0'){
            return "0";
        }
        String ret = "0";
        int len1 = num1.length();
        int len2 = num2.length();
        // 模拟列竖式运算
        for(int i = len1 - 1; i >= 0; i--){
            int tmp = 0;
            StringBuilder s = new StringBuilder();
            int k = len1 - 1 - i;
            while(k > 0){
                s.append('0');
                k--;
            }
            for(int j = len2 - 1; j >= 0; j--){
                tmp += (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
                s.append(tmp % 10);
                tmp /= 10;
            }
            if(tmp > 0){
                s.append(tmp);
            }
            ret = twoStringAdd(ret, s.toString());
        }

        return new StringBuilder(ret).reverse().toString();
    }
    private String twoStringAdd(String s1, String s2){
        int t = 0;
        StringBuilder s = new StringBuilder();
        int i = 0;
        int j = 0;
        while(i < s1.length() || j < s2.length() || t > 0){
            if(i < s1.length()){
                t += (s1.charAt(i) - '0');
                i++;
            }
            if(j < s2.length()){
                t += (s2.charAt(j) - '0');
                j++;
            }
            s.append(t % 10);
            t /= 10;
        }
        return s.toString();
    }
}